package com.aloda.orangebank.service.impl;

import com.aloda.orangebank.mapper.UserMapper;
import com.aloda.orangebank.pojo.ChangePwd;
import com.aloda.orangebank.pojo.User;
import com.aloda.orangebank.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int setUserPwd(ChangePwd changePwd) {
        if(Objects.equals(changePwd.getNewPwd(), changePwd.getOldPwd())){
            return 0;
        }
//        User u = getByUserId(changePwd.getId());因为调用了service层的查找用户方法，导致返回的password为经过处理的值，正确方法应该直接调用mapper层的方法
//        System.out.println(u.getPassword());
//        System.out.println(changePwd.getOldPwd());
        User u = userMapper.getByUserId(changePwd.getId());
        if(!Objects.equals(u.getPassword(), changePwd.getOldPwd())){
            return 1;
        }
        userMapper.updatePwd(changePwd);
        return 2;
    }

    @Override
    public int setUserInfo(User user) {
        //验证修改后的用户名是否已经存在的逻辑
        User u = userMapper.getUserByUsername(user.getUsername());
        if(u!=null&&!Objects.equals(u.getId(), user.getId())){
            return 2;
        }
        user.setUpdateTime(LocalDateTime.now());
        if(userMapper.updateUser(user)){
            return 1;
        };
        return 0;
    }

    @Override
    public boolean register(User user) {
        //为用户定义随机的昵称
        if(user.getName().equals("")){
//            System.out.println("随机名已执行");
            String secondString = "土豆";
            String firstString=" "; // 初始化firstString
            Random random = new Random();
            int r = random.nextInt(5);
            int t = random.nextInt(5);
            switch (r) {
                case 0 -> firstString = "飞翔的";
                case 1 -> firstString = "游泳的";
                case 2 -> firstString = "奔跑的";
                case 3 -> firstString = "会飞的";
                case 4 -> firstString = "睡觉的";
                // 不需要default，因为所有可能的值都已被覆盖
            }
            switch (t) {
                case 0 -> secondString = "土豆";
                case 1 -> secondString = "西瓜";
                case 2 -> secondString = "杨桃";
                case 3 -> secondString = "草莓";
                case 4 -> secondString = "西红柿";
                // 不需要default，因为所有可能的值都已被覆盖
            }
            user.setName(firstString + secondString); // 拼接昵称并设置
        }
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        if(userMapper.getByUsername(user.getUsername())){
            return false;
        }else {
            userMapper.register(user);
            return true;
        }
    }

    @Override
    public User getByUserId(Integer id) {
        User u = userMapper.getByUserId(id);
        u.setPassword("??????");
        return u;
    }

    @Override
    public User login(User user) {
        return userMapper.getByUP(user);
    }

//    public boolean getByUsername(String username){
//        if()
//        return false;
//    }
}
